<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/6
 * Time: 13:29
 */

namespace App\Http\Middleware;

use Closure;

class CheckAccountLogin
{
    public function handle($request, Closure $next)
    {
        if (!(is_account_login() && session('menus'))) {
            return redirect(urlAccount('login', 'getIndex'));
        }

        if (!empty(C('ADMIN_ALLOW_IP'))) {
            // 检查IP地址访问
            if (!in_array(request()->getClientIp(), explode(',', C('ADMIN_ALLOW_IP')))) {
                return $this->dealResult('禁止访问！');
            }
        }
        return $next($request);
    }

    public function dealResult($msg)
    {
        if (request()->ajax()) {
            return error($msg);
        } else {
            abort(503, $msg);
        }
    }

}

